#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

#define MAX_N 1000
long long dp[MAX_N + 5][MAX_N + 5] = {0};
int nums[MAX_N + 5];

int main()
{
    int n, k;
    memset(dp, 0, sizeof(dp));
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> nums[i];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < k; j++)
        {
            // 排除首行
            if (i == 0)
            {
                if(j != 0) break;
                int index = (j + nums[i]) % k;
                dp[i][index] = nums[i];
                break;
            }
            // 正常逻辑
            if (j != 0 && dp[i - 1][j] == 0)
                continue;
            dp[i][j] = max(dp[i][j], dp[i - 1][j]);
            int index = (j + nums[i]) % k;
            // cout << "index: " << index << endl;
            dp[i][index] = max(dp[i - 1][index], dp[i - 1][j] + nums[i]);
        }
        // cout << "---------------------------------" << endl;
        // for (int j = 0; j < k; j++)
        //     cout << dp[i][j] << " ";
        // cout << endl;
        // cout << "---------------------------------" << endl;
    }
    cout << dp[n - 1][0];
    return 0;
}